package com.gzyj.web.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.ResponseMsg;
import com.gzyj.data.web.cljk.VehVideoPlayBackGetRSParam;
import com.gzyj.data.web.cljk.VehVideoPlayBackParam;
import com.gzyj.pojo.gps.GzyjVideoHistory;
import com.gzyj.web.mapper.gps.GzyjVideoHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("api/video/history")
public class VideoApi {

    @Autowired
    private GzyjVideoHistoryMapper gzyjVideoHistoryMapper;

    @Value("${kafka.queue.down}")
    private String queueDown;

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Autowired
    RedisTemplate<String, Object> clusterRedisTemplate;

    @Value("${redis.key.vehicle.cmdrs}")
    private String VehicleZdCMDRS;


    @PostMapping("send")
    public ResponseMsg<Object> send(VehVideoPlayBackParam videoPlayBackParam) {

        GzyjVideoHistory videoHistory = gzyjVideoHistoryMapper.selectOne(new QueryWrapper<GzyjVideoHistory>()
                .eq("simNo", videoPlayBackParam.getSimNo())
                .eq("logicChannelNo", videoPlayBackParam.getChannelNo())
                .eq("beginTime", videoPlayBackParam.getStartTime())
                .eq("endTime", videoPlayBackParam.getEndTime()));

        if (videoHistory != null) {
            clusterRedisTemplate.delete(VehicleZdCMDRS + videoPlayBackParam.getSimNo() + ":" + 0x1206 + ":" + videoHistory.getSerialNum());
            JSONObject param = new JSONObject();
            param.put("cmdID", 0x9206);
            param.put("ipAddress", videoPlayBackParam.getSimNo());

            param.put("logicChannelNo", videoPlayBackParam.getChannelNo());
            param.put("beginTime", videoHistory.getBeginTime());
            param.put("endTime", videoHistory.getEndTime());
            param.put("alarmFlag", videoHistory.getAlarmFlag());
            param.put("aVResourceType", videoHistory.getAVResourceType());
            param.put("streamType", videoHistory.getStreamType());
            param.put("memoryType", videoHistory.getMemoryType());


            param.put("seq", videoHistory.getSerialNum());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            param.put("sendTime", sdf.format(new Date()));
            param.put("storagePath",videoHistory.getStoragePath());
            kafkaTemplate.send(queueDown, JSON.toJSONString(param, SerializerFeature.DisableCircularReferenceDetect));

            return new ResponseMsg<>(200, "success", videoHistory.getSerialNum());
        }
        return new ResponseMsg<>(201, "不存在该记录");
    }

    @PostMapping("getZdVideoRS")
    public ResponseMsg<Object> getZdVideoRS(VehVideoPlayBackGetRSParam vehVideoPlayBackGetRSParam) {

        Object obj = clusterRedisTemplate.opsForValue().get(VehicleZdCMDRS + vehVideoPlayBackGetRSParam.getSimNo() + ":" + 0x1206 + ":" + vehVideoPlayBackGetRSParam.getVerifyCode());

        if (obj != null) {
            return new ResponseMsg<>(200, "success");
        }

        return new ResponseMsg<>(201, "终端未响应");
    }


}
